package com.cinema.dao;

import com.cinema.entity.Hall;
import com.cinema.util.DBUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 影厅数据访问对象
 */
public class HallDAO {

    /**
     * 根据影院ID获取影厅列表
     */
    public List<Hall> findByCinemaId(Integer cinemaId) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        List<Hall> halls = new ArrayList<>();

        try {
            conn = DBUtil.getConnection();
            String sql = "SELECT * FROM halls WHERE cinema_id = ? AND (status = 'ACTIVE' OR status IS NULL) ORDER BY name";
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, cinemaId);
            rs = pstmt.executeQuery();

            while (rs.next()) {
                Hall hall = mapResultSetToHall(rs);
                halls.add(hall);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(conn, pstmt, rs);
        }

        return halls;
    }

    /**
     * 根据ID获取影厅
     */
    public Hall findById(Integer id) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        Hall hall = null;

        try {
            conn = DBUtil.getConnection();
            String sql = "SELECT * FROM halls WHERE id = ?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, id);
            rs = pstmt.executeQuery();

            if (rs.next()) {
                hall = mapResultSetToHall(rs);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(conn, pstmt, rs);
        }

        return hall;
    }

    /**
     * 获取所有影厅列表
     */
    public List<Hall> findAll() {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        List<Hall> halls = new ArrayList<>();

        try {
            conn = DBUtil.getConnection();
            String sql = "SELECT h.*, c.name as cinema_name FROM halls h " +
                        "LEFT JOIN cinemas c ON h.cinema_id = c.id " +
                        "WHERE (h.status = 'ACTIVE' OR h.status IS NULL) ORDER BY c.name, h.name";
            pstmt = conn.prepareStatement(sql);
            rs = pstmt.executeQuery();

            while (rs.next()) {
                Hall hall = mapResultSetToHall(rs);
                hall.setCinemaName(rs.getString("cinema_name"));
                halls.add(hall);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            DBUtil.close(conn, pstmt, rs);
        }

        return halls;
    }

    /**
     * 将ResultSet映射为Hall对象
     */
    private Hall mapResultSetToHall(ResultSet rs) throws SQLException {
        Hall hall = new Hall();
        hall.setId(rs.getInt("id"));
        hall.setCinemaId(rs.getInt("cinema_id"));
        hall.setName(rs.getString("name"));
        hall.setTotalSeats(rs.getInt("total_seats"));
        hall.setSeatLayout(rs.getString("seat_layout"));
        hall.setStatus(rs.getString("status"));
        hall.setCreatedAt(rs.getTimestamp("created_at"));
        return hall;
    }
}
